properties { 
  $base_dir  = resolve-path .
  $source_dir = "$base_dir/source"  
  $build_artifacts_dir = "$base_dir/artifacts"
  $code_analysis_dir = "$build_artifacts_dir/code_analysis"
  $release_dir =  "$base_dir/Release"
  $build_output_dir = "$source_dir/bin"
  $temp_build_output_dir = "$build_output_dir/tmp"
  $log_dir = "$build_artifacts_dir/docs"
  
  ## Tool Directories
  $build_tools_dir = "$base_dir/build/tools"
  $fxcop_dir = "$build_tools_dir/Microsoft Fxcop 10.0"
  $psake_dir = "$build_tools_dir/psake"
  $ilmerge_dir = "$build_tools_dir/ILMerge"
  $msbuild_dir = "$build_tools_dir/MSBuild"
  
  ## FxCop configuration
  $fxcop_output = "$source_dir/code_analysis/analysis.html"
  $fxcop_xsl = "$fxcop_dir/xml/CodeAnalysisReport.xsl"
  
  $merged_output_dll_name = "Meranko"
  
  $lib_dir = "$source_dir\third_party"
  $build_dir = "$source_dir\build"
  $sln_file = "Solution.sln"
}

task default -depends Test

## Code Analysis Tasks
task CleanCodeAnalysis {
    remove-item -force -recurse $code_analysis_dir -ErrorAction SilentlyContinue
}

task CodeAnalysis -depends CleanCodeAnalysis, Build {
    new-item $code_analysis_dir -itemType directory
    & "$fxcop_dir/FxCopCmd" /f:"$build_output_dir/*.dll" `
        /out:$fxcop_output `
        /outXsl:$fxcop_xsl `
        /applyoutXsl
}

## Build Versioning Tasks
task BuildVersion -depends Build, Merge, Test, CodeAnalysis {
    . "$psake_dir/psake_extension_library.ps1"
    Get-ChildItem -path "$source_dir/*/Properties" -include "AssemblyInfo.cs" -r | Foreach-Object { 
        $file_name = Resolve-Path($_.Directory.ToString() + '/' + $_.Name)
        increment-version $file_name
    }
}

task ReleaseVersion -depends Build, Merge {
    . "$psake_dir/psake_extension_library.ps1"
    Get-ChildItem -path "$source_dir/*/Properties" -include "AssemblyInfo.cs" -r | Foreach-Object { 
        $file_name = Resolve-Path($_.Directory.ToString() + '/' + $_.Name)
        increment-version $file_name $true
    }
}

## Build Tasks
task CleanBuild {
    remove-item -force -r $build_artifacts_dir -ErrorAction SilentlyContinue
    remove-item -force -r $build_output_dir -ErrorAction SilentlyContinue
}

task Build -depends CleanBuild, BuildThirdParty { 
    new-item $build_artifacts_dir -itemType directory
    new-item $build_output_dir -itemType directory
    new-item $temp_build_output_dir -itemType directory 
    new-item $log_dir -itemType directory 
    . "$msbuild_dir/MSBuild.exe" /p:OutDir="$temp_build_output_dir/" "$source_dir/$sln_file"
}

## Libraries/Third Party Tasks
task CleanBuildThirdParty {
    
}

task BuildThirdParty -depends CleanBuildThirdParty {
    
}

## Release Task
task CleanRelease {
    remove-item -force -recurse $release_dir -ErrorAction SilentlyContinue
}

task Release -depends ReleaseVersion, CleanRelease {
    new-item $release_dir -itemType directory
    & $tools_dir\zip.exe -9 -A -j `
        $release_dir\Rhino.Mocks.zip `
        $build_output_dir\Rhino.Mocks.dll `
        $build_output_dir\Rhino.Mocks.xml `
        license.txt `
        acknowledgements.txt `
    if ($lastExitCode -ne 0) {
        throw "Error: Failed to execute ZIP command"
    }
}

## Test Task
task Test -depends Build {
    #$old = pwd
    #cd $build_dir
    #exec ".\MbUnit.Cons.exe" "$build_dir\Rhino.Mocks.Tests.dll"
    #cd $old        
}

task Coverage -depends Test {
    
}

## Merge Task
task Merge -depends Build {
    new-item $log_dir/merge.txt -itemType file
   . "$ilmerge_dir\ILMerge.exe" /wildcards `
        /log:"$log_dir/merge.txt" `
        /target:library `
        /out:"$build_output_dir/Meranko.dll" `
        "$temp_build_output_dir/*.dll"
    if ($lastExitCode -ne 0) {
        throw "Error: Failed to merge assemblies!"
    }    
    Remove-Item -R $temp_build_output_dir -ErrorAction SilentlyContinue
}